import $service from '../../../../service.js';
var hospital_id="1";
var that;
var dataId='';
var isCheck=true;
var obj = {};
var doctor_id;
var log_id;
var checklistId;
var checklistListAll;
var typeNum;
Page({
    data:{
       isClassList : 1,
       checklistClassList :[],
       nextClassList : [],
       checklistList: [],
       dataId:'',//一级
       dataId2:'',//二级
       opinionList:[],//
       opinionList2:[],
       opinionList3:[],
       inquiryMap:''
    },
    onLoad(e){
        that=this.data;
        log_id = e.log_id;
    },
    onShow(){
        doctor_id = $service.getUserLocalStorage().doctor_id;
        if(!$service.isUndefinedAndEmpty($service.getUserLocalStorage().hospital_id)){
            hospital_id = $service.getUserLocalStorage().hospital_id
        }
        isCheck=true;
        that=this.data;
        this.doctorSide_personal_inquiry_getUtilChecklistClass();
        this.publicUtil_getInquiryLogDetails()
    },
    //问诊详情
    publicUtil_getInquiryLogDetails:function(){
        var jsonData ={
            "is_receive":"1",
            "log_id" : log_id
        }
        var jsonAjax = {
            "url": "publicUtil_getInquiryLogDetails.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.publicUtil_getInquiryLogDetails_back);
    },
    //回调
    publicUtil_getInquiryLogDetails_back: function(jsonBack){
         var data=$service.strToJson(jsonBack.data).data;
         var inquiryMap= data.inquiryMap; //患者信息
         var doctorMap = data.doctorMap; //医生信息
         checklistListAll = data.checklistList;
         that.opinionList3 = []
         for(var i = 0; i < data.checklistList.length; i++){
             if(data.checklistList[i].checklist_type==1){//类型 checklist_type== 1 检验检查  查opinionList opinionList2 里的检验相关项目
                 if($service.isUndefinedAndEmpty(wx.getStorageSync('checklistListAll' + log_id))){
                     that.opinionList = []
                     that.opinionList2 = []
                     checklistId = data.checklistList[i].checklist_id;
                      var jsonData={
                         "checklist_id" : data.checklistList[i].checklist_id
                     }
                     var jsonAjax = {
                         "url": "publicUtil_getInquiryLogChecklistDetails.do",
                         "jsonData": jsonData,
                     }
                     $service.getAjaxData(jsonAjax, this.publicUtil_getInquiryLogChecklistDetails_back);
                 }
                 
             }else{
                 if(data.checklistList[i].checklist_type==2||data.checklistList[i].checklist_type==3){
                     that.opinionList3.push(data.checklistList[i]);
                 }
             }
         }
         if (!$service.isUndefinedAndEmpty(inquiryMap.lof_diagnosis)) {
             var lof_diagnosis = '';
             var lof_diagnosis2 = '';
             var numC =0
             var lofdiagnosislist = inquiryMap.lof_diagnosis.split(",")
             for (var i = 0; i < lofdiagnosislist.length; i++) {
                 var index = lofdiagnosislist[i].indexOf('自定义诊断');
                 if (index > -1) {
                     lof_diagnosis2 += lofdiagnosislist[i] + ",";
                 } else {
                     numC++
                     lof_diagnosis += "(" + numC + ")" + lofdiagnosislist[i] + ",";
                 }
             }
             inquiryMap.diagnosisMap = lof_diagnosis + lof_diagnosis2;
             inquiryMap.diagnosisMap = inquiryMap.diagnosisMap.substring(0, inquiryMap.diagnosisMap.length - 1);
         } else {
             inquiryMap.diagnosisMap =''
         }
         this.setData({
             opinionList3 :that.opinionList3,
             inquiryMap: inquiryMap
         }) 
    },
    
    //已生成检验检查单
    publicUtil_getInquiryLogChecklistDetails_back(jsonBack){
        var data=$service.strToJson(jsonBack.data).data;
        var checklistMap = data.checklistMap;//检验信息
        var checklistDetailsList = checklistMap.checklistDetailsList;//检验列表
        for(var i = 0; i < checklistDetailsList.length; i++){
            if(checklistDetailsList[i].checklist_type==1){
                that.opinionList.push(checklistDetailsList[i]);
            }
            if(checklistDetailsList[i].checklist_type !=1){
                that.opinionList2.push(checklistDetailsList[i]);
            }
        }
        this.setData({
            opinionList :that.opinionList,
            opinionList2 :that.opinionList2,
            lof_proposal:checklistMap.treatment_suggestion
        }) 
    },
    
    //数据
    doctorSide_personal_inquiry_getUtilChecklistClass:function(){
        var jsonData ={
            "hospital_id":hospital_id,
            "checklist_class_id":dataId
        }
        var jsonAjax = {
            "url": "doctorSide_personal_inquiry_getUtilChecklistClass.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_inquiry_getUtilChecklistClass_back);
    },
    //回调
    doctorSide_personal_inquiry_getUtilChecklistClass_back: function(jsonBack){
         var data=$service.strToJson(jsonBack.data).data;
         var checklistClassList= data.checklistClassList; //大分类
         for(var i = 0; i < checklistClassList.length; i++){
            checklistClassList[i].isshow = false;
         }
         if(data.isClassList==1){//是否返回分类list  0否 1是
             that.nextClassList = data.nextClassList;//下级分类列表
             for(var i = 0; i < that.nextClassList.length; i++){
                that.nextClassList[i].istwoshow = false;
             }
         }else{
             if(!$service.isUndefinedAndEmpty(data.checklistList)){
                that.checklistList = data.checklistList; //下级分类列表
             }
             if(!$service.isUndefinedAndEmpty(that.dataId2)){
                that.checklistList = data.checklistList; //下级分类列表
             }
         }
       if(!$service.isUndefinedAndEmpty(that.dataId)){//一级列表点击展开/隐藏
           this.setData({
               nextClassList : that.nextClassList,
               checklistList :that.checklistList,
               isClassList:data.isClassList
           })
          
       }else if(!$service.isUndefinedAndEmpty(that.dataId2)){//二级列表点击展开/隐藏
          this.setData({
              checklistList :that.checklistList,
              isClassList:data.isClassList
          })  
        }else {
            this.setData({
                checklistClassList :checklistClassList,
                nextClassList : that.nextClassList,
                checklistList :that.checklistList,
                isClassList:data.isClassList
            }) 
        }
        
         
    },
    //列表点击事件
    checklistClasstap(e){
        console.log(e)
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        if(num ==1){//1级列表点击
            dataId = that.checklistClassList[index].checklist_class_id;
            for(var i = 0; i < that.checklistClassList.length; i++){
                if(that.checklistClassList[i].checklist_class_id == dataId){
                    that.checklistClassList[i].isshow = !that.checklistClassList[i].isshow;
                }else{
                    that.checklistClassList[i].isshow = false;
                }
                
            }
            this.setData({
                dataId :dataId,
                checklistClassList: that.checklistClassList,
                nextClassList:[],
                checklistList :[]
            })
            this.doctorSide_personal_inquiry_getUtilChecklistClass()
        }else if(num == 2){//二级列表点击
            that.dataId2 = that.nextClassList[index].checklist_class_id;
            dataId = that.dataId2;
            for(var i = 0; i < that.nextClassList.length; i++){
                if(that.nextClassList[i].checklist_class_id == that.dataId2){
                    that.nextClassList[i].istwoshow = !that.nextClassList[i].istwoshow;
                }else{
                    that.nextClassList[i].istwoshow = false;
                }
                
            }
            this.setData({
                dataId2 :that.dataId2,
                nextClassList: that.nextClassList,
                checklistList :[]
            })
            this.doctorSide_personal_inquiry_getUtilChecklistClass()
        }
        
    },
    //添加至检验检查项目
    addOpinion(e){
        if($service.isUndefinedAndEmpty(that.inquiryMap.log_main_suit)){
            $service.showToast("请先输入主诉")
            return;
        }
        if($service.isUndefinedAndEmpty(that.inquiryMap.diagnosisMap) ){
            $service.showToast("请先输入诊断")
            return;
        }
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        var type = e.currentTarget.dataset.type;
        if(num == 1){//检验检查
            if(type == 1){
                if(that.opinionList.length > 4){
                    $service.showToast('最多五个');
                    return;
                }
                for(var i = 0; i < that.opinionList.length;i++){
                    if(that.checklistList[index].checklist_id == that.opinionList[i].checklist_id){
                        $service.showToast('请不要重复添加');
                        return;
                    }
                }
                that.opinionList.push(that.checklistList[index])
            }else{
                if(that.opinionList2.length > 4){
                    $service.showToast('最多五个');
                    return;
                }
                for(var i = 0; i < that.opinionList2.length;i++){
                    if(that.checklistList[index].checklist_id == that.opinionList2[i].checklist_id){
                        $service.showToast('请不要重复添加');
                        return;
                    }
                }
                that.opinionList2.push(that.checklistList[index])
            }
        }else if(num == 2){//新增申请单
            var i = e.currentTarget.dataset.i;
            var checklistList = that.checklistList[index];
            var checklist_class_id =that.checklistClassList[i].checklist_class_id;
            var objMap={
                inquiryMap : that.inquiryMap,
                checklistList:checklistList,
                checklist_class_id:checklist_class_id,
                checklistListAll:checklistListAll
            }
            wx.setStorageSync('checklistListAll' + log_id,"1")
            if(type == 3){
                $service.openPage('index_electrocardiogram/index_electrocardiogram?obj=' + $service.jsonToStr(objMap))
            }else{
                $service.openPage('index_inspectioninspection_apply/index_inspectioninspection_apply?obj=' + $service.jsonToStr(objMap))
            }
            
        }
        
        this.setData({
            opinionList :that.opinionList,
            opinionList2 :that.opinionList2
        })
    },
    //删除检验项目
    delopinionList(e){
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        if(num == 1){
            that.opinionList.splice(index,1)
        }else if(num == 2){
             that.opinionList2.splice(index,1)
        }else{
            if(!$service.isUndefinedAndEmpty(that.opinionList3[index].checklist_id)){
                var jsonData = {
                	"checklist_id":that.opinionList3[index].checklist_id
                }
                var jsonAjax = {
                    "url": "doctorSide_personal_inquiry_deleteInquiryLogChecklist.do",
                    "jsonData": jsonData,
                }
                $service.getAjaxData(jsonAjax, this.deleteInquiryLogChecklist_back);
                that.opinionList3.splice(index,1);
            }
        }
        this.setData({
            opinionList :that.opinionList,
            opinionList2 :that.opinionList2,
            opinionList3 :that.opinionList3
            
        })
    },
    //删除回调
    deleteInquiryLogChecklist_back(jsonBack){
        var selToID='';
        if(that.inquiryMap.join_type == 1){
             selToID = "U" + that.inquiryMap.join_id
        }else{
              selToID = "S" + that.inquiryMap.join_id
        }
        var sendSystemMessageMsgText = '医生已撤销检验检查单'
        wx.txTic.sendCustomMessage(selToID,6)
        wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
        wx.showToast({"title":'删除成功',"icon":"none",duration: 4000});
        wx.setStorageSync('checklistListAll' + log_id,"1")
        this.publicUtil_getInquiryLogDetails()
    },
    //保存
    addInspectionBox(){
        if($service.isUndefinedAndEmpty(that.inquiryMap.log_main_suit)){
            $service.showToast("请先输入主诉")
            return;
        }
        if($service.isUndefinedAndEmpty(that.inquiryMap.diagnosisMap) ){
            $service.showToast("请先输入诊断")
            return;
        }
        
        var detailsList = that.opinionList.concat(that.opinionList2);
        if(!$service.isUndefinedAndEmpty(that.lof_proposal)){
            if(detailsList.length == 0){
                $service.showToast("有填写医生建议，就必须选择检验检查项目！") 
                return;
            }
        }
        if(detailsList.length != 0){
            if($service.isUndefinedAndEmpty(that.lof_proposal)){
                $service.showToast("请填写医生建议") 
                return;
            }
        }
        
        for(var i = 0; i < detailsList.length;i++){
            detailsList[i].checklistType = 1;
        }
        if(detailsList.length>0){
        		var jsonData={  
                    "checklistType":1,
                    "checklist_id":checklistId,
                    "log_id":that.inquiryMap.log_id,
                    "user_type":that.inquiryMap.join_type,
                    "user_id":that.inquiryMap.join_id,
                    "inquiry_patient_id":that.inquiryMap.inquiry_patient_id,
                    "doctor_id":doctor_id,
                    "hospital_id":hospital_id,
                    "checklist_inspection_result":that.checklist_inspection_result,//有关检验结果
                    "detailsList":$service.jsonToStr(detailsList),
                    "lof_proposal":that.lof_proposal
                }
                console.log("detailsList==="+$service.jsonToStr(jsonData))
                var jsonAjax = {
                    "url": "doctorSide_personal_inquiry_addInquiryLogCheckList.do",
                    "jsonData": jsonData,
                }
                $service.getAjaxData(jsonAjax, this.doctorSide_personal_inquiry_addInquiryLogCheckList_back);
        	}else{
                var jsonData = {
                	"checklist_id":checklistId,
                    "lof_proposal":that.lof_proposal
                }
                var jsonAjax = {
                    "url": "doctorSide_personal_inquiry_deleteInquiryLogChecklist.do",
                    "jsonData": jsonData,
                }
                $service.getAjaxData(jsonAjax, this.doctorSide_personal_inquiry_addInquiryLogCheckList2_back);
        	}
    },
    doctorSide_personal_inquiry_addInquiryLogCheckList_back(jsonBack){
        var selToID='';
        if(that.inquiryMap.join_type == 1){
             selToID = "U" + that.inquiryMap.join_id
        }else{
              selToID = "S" + that.inquiryMap.join_id
        }
        var sendSystemMessageMsgText = '医生已开具检验检查单'
        wx.txTic.sendCustomMessage(selToID,6)
        wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
        wx.showToast({"title":'保存成功',"icon":"none",duration: 4000});
        setTimeout(function(){
            wx.navigateBack({
                delta:1
            })
        },1500);
    },
    doctorSide_personal_inquiry_addInquiryLogCheckList2_back(jsonBack){
        var selToID='';
        if(that.inquiryMap.join_type == 1){
             selToID = "U" + that.inquiryMap.join_id
        }else{
              selToID = "S" + that.inquiryMap.join_id
        }
        var sendSystemMessageMsgText = '医生已撤销检验检查单'
        wx.txTic.sendCustomMessage(selToID,6)
        wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
        wx.showToast({"title":'撤销成功',"icon":"none",duration: 4000});
        setTimeout(function(){
            wx.navigateBack({
                delta:1
            })
        },1500);
    },
    //监听输入框
    inputChange(e){
        that.lof_proposal = e.detail.value
        this.setData({
            lof_proposal : that.lof_proposal
        })
    },
    gotodetalis(e){
        var index = e.currentTarget.dataset.index;
        if(index == 0 || index == 1 || index == 2 || index == 3 ||index == 4 ){//0过敏史 1 主诉 2现病史 3既往史 4诊断
            $service.openPage('../index_diagnosis/index_diagnosis?inquiryMap=' + $service.jsonToStr(that.inquiryMap) +'&index='+ index)
        }
    }
})